package com.g2room.driving.grap.life;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.ComShop;
import com.g2room.driving.grap.life.model.po.LifeFood;
import com.g2room.driving.grap.life.model.po.LifeFoodShop;
import com.g2room.driving.grap.life.model.po.LifeScenery;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.util.DbUtil;
import com.g2room.myuall.util.StringUtil;

public class GrabCncnJingdian {
	public String baseUrl = "http://you.ctrip.com";
	private String source = "携程";
	private static AtomicInteger id = new AtomicInteger(getMaxId());
	private static Map<String, Integer> typeMap = new HashMap<String, Integer>();
	public static void main(String[] args) throws Exception{
		GrabCncnJingdian gkd = new GrabCncnJingdian();
//		gkd.getMeijing("http://chongqing.cncn.com/jingdian/1-#ps#-0-0.html", 20);
		
//		List<GrapErr> errs = getErrs();
//		for(GrapErr ge : errs) {
//			if(ge.getType().equals("1")) {
//				gkd.getMeiJingList(ge.getUrl(), "美景");
//			} else if(ge.getType().equals("2")) {
//				
//			}
//		}
		
//		gkd.getMeiShiList("http://chongqing.cncn.com/meishi/", "美食");
		gkd.getMeiShiList("http://chongqing.cncn.com/techan/", "特产");
		
//		gkd.getChanguan("http://you.ctrip.com/restaurantlist/chongqing158/s0-p#ps#.html", 100);
//		gkd.getSpecialty("http://you.ctrip.com/goods/chongqing158/s0-p#ps#.html", 1); // 特产
//		gkd.getShopping("http://you.ctrip.com/shoppinglist/chongqing158/s0-p#ps#.html", 5);
		
//		Map<Integer, String> lsids = getSensList();
//		for(Integer id : lsids.keySet()) {
//			gkd.getScore(id, lsids.get(id));
//		}
		
    }
	private int tid = 10016;
	private int getTypeId() {
		return tid++;
	}
	
//	private static synchronized int getId() {
//		id.addAndGet(1);
//		return id.get();
//	}
	
	private void getScore(int id, String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("8");
			ge.setContent(id+"");
			ge.setDescription("获取Score 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		LifeScenery li = new LifeScenery();
		li.setId(id);
		Elements els = doc.select("dl.comment_show dd");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				String text = el.text();
				if(text.contains("景")) {
					li.setScenery(text.replace("景&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;色", "").replace("景 色 ", ""));
				} else if(text.contains("趣")) {
					li.setInterest(text.replace("趣&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;味", "").replace("趣 味", ""));
				} else if(text.contains("性价比")) {
					li.setCost(text.replace("性价比", ""));
				}
			}
			
			updateLs(li);
		}
	}
	/** * 美景 */
	public void getMeijing(String url, int pagesize) {
		for(int i = 1; i < pagesize+1; i++) {
			getMeiJingList(url.replace("#ps#", i+""), "美景");
		}
	}

	/**
	 * 餐馆
	 * @param url
	 * @param pagesize
	 */
	public void getChanguan(String url, int pagesize) {
		for(int i = 1; i < pagesize+1; i++) {
			getCanguanList(url.replace("#ps#", i+""), "餐馆小吃");
		}
	}
	/** 特产 */
	public void getSpecialty(String url, int pagesize) {
		for(int i = 1; i < pagesize+1; i++) {
			getMeiShiList(url.replace("#ps#", i+""), "特产");
		}
	}
	/** 购物 */
	public void getShopping(String url, int pagesize) {
		for(int i = 1; i < pagesize+1; i++) {
			getCanguanList(url.replace("#ps#", i+""), "购物");
		}
	}
	
	public void getCanguanList(String url, String kind) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(kind);
			ge.setDescription("获取List 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		List<ComShop> css = new ArrayList<ComShop>();
		Elements els = doc.select("div.list_mod2");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				ComShop cs = new ComShop();
				cs.setKindName(kind);
				// 获取封面
				Elements tempEls = el.select(".leftimg img");
				if(null != tempEls && !tempEls.isEmpty()) {
					String src = tempEls.first().attr("data-imgurl");
					if(StringUtil.isBlank(src)) {
						src = tempEls.first().attr("src");
					}
					cs.setCover(src);
				}
				
//				tempEls = el.getElementsByAttributeValue("class", "rdetailbox");
				tempEls = el.select("div.rdetailbox dl dt a");
				if(null != tempEls && !tempEls.isEmpty()) {
					// 获取名称和链接
					cs.setFullName(tempEls.first().text());
					
					if(cs.getFullName().contains("(")) {
						cs.setName(cs.getFullName().substring(0, cs.getFullName().indexOf("(")));
						cs.setSecondName(cs.getFullName().substring(cs.getFullName().indexOf("(") + 1, cs.getFullName().length() - 1));
					} else {
						cs.setName(cs.getFullName());
					}
					
					cs.setSourceUrl(baseUrl + tempEls.first().attr("href"));
					cs.setOldId(cs.getSourceUrl().substring(cs.getSourceUrl().lastIndexOf("/")+1).replace(".html", ""));
					cs.setSource(source);
				}
				tempEls = el.select("div.rdetailbox ul li .score strong");
				if(null != tempEls && !tempEls.isEmpty()) {
					// 获取得分
					cs.setStar(tempEls.first().text());
				}
//				cs.setId(getId());

				css.add(cs);
			}
		}
		for(ComShop cs : css) {
			getCanguanDetail(cs);
		}
		
		if(css.size() > 0) insert2ShopDb(css);
	}

	public ComShop getCanguanDetail(ComShop cs) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(cs.getSourceUrl());
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(cs.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(cs));
			ge.setDescription("获取Detail 错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		try {
			Elements els = doc.select("ul.s_sight_in_list li");
			if(null != els && !els.isEmpty()) {
				for(Element el  : els) {
					String text = el.text();
					if(text.contains("人")) {
						cs.setSpendPerhead(text.replace("人 均：", "").replace("¥", ""));
					} else if(text.contains("菜") || text.contains("类")) {
						cs.setCategoryName(text.replace("菜 系：", "").replace("类        型：", ""));
					} else if(text.contains("地")) {
						cs.setAddress(text.replace("地 址：", "").replace("地        址：", ""));
					} else if(text.contains("电")) {
						cs.setPhone(text.replace("电 话：", "").replace("电        话：", ""));
					} else if(text.contains("营业时间")) {
						cs.setShopHours(text.replace("营业时间：", ""));
					}
				}
			}
			
			els = doc.select("ul.s_sight_in_list dt");
			if(null != els && !els.isEmpty()) {
				if(els.text().contains("营业时间")) {
					cs.setShopHours(doc.select("ul.s_sight_in_list dd").first().text());
				}
			}
				
			els = doc.select("div.normalbox .detailcon");
			if(null != els && !els.isEmpty()) {
				String lastTitle = "";
				for(Element el : els.first().children()) {
					if(el.tagName().equalsIgnoreCase("h2")) {
						lastTitle = el.text();
					} else if(lastTitle.contains("介绍") || lastTitle.contains("简介")) {
						if(null != el.attr("class") && el.attr("class").contains("toggle_s")) continue;
						cs.setDescription(el.text());
						lastTitle = "";
					} else if(lastTitle.contains("美食")) {
						cs.setMenu(el.text());
						lastTitle = "";
					} else if(el.attr("class").contains("detailcon")) {
						if(el.id().equals("mapCanvas")) continue;
						cs.setTraffic(el.text().replace("交通", ""));
						lastTitle = "";
					}
				}
//					if(els.size() > 1) {
//						cs.setMenu(els.get(1).text());
//					}
//					cs.setDescription(els.first().text());
			}
			
			els = doc.select("dl.comment_show");
			if(null != els && !els.isEmpty()) {
				for(Element el : els.first().children()) {
					String text = el.text();
					if(text.contains("服务")) {
						cs.setService(text.replace("服务", ""));
					} else if(text.contains("环境")) {
						cs.setEnvironment(text.replace("环境", ""));
					} else if(text.contains("口味")) {
						cs.setFlavor(text.replace("口味", ""));
					} else if(text.contains("商品")) {
						cs.setFlavor(text);
					}
				}
			}
			
			if(StringUtil.isBlank(cs.getCover())) {
				els = doc.select("#detailCarousel img");
				if(null != els && !els.isEmpty()) {
					cs.setCover(els.first().attr("src"));
				}
			}
			return cs;
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(cs.getSourceUrl());
			ge.setType("4");
			ge.setContent(JSON.toJSONString(cs));
			ge.setDescription("获取Detail 内容错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
	}
	
	public void getMeiJingList(String url, String kind) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(kind);
			ge.setDescription("获取List 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		Elements els = doc.select(".city_spots_list ul li .pic");
		List<LifeScenery> lses = new ArrayList<LifeScenery>();
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
//				dUrls.add(el.attr("href"));
				LifeScenery ls = new LifeScenery();
				ls.setSourceUrl(el.attr("href"));
				ls.setCover(el.select("img").first().attr("data-original"));
				ls.setKindName("美景");
				// 获取简称
				ls.setName(el.select(".title b").text());
				lses.add(ls);
			}
		}
		for(LifeScenery ls : lses) {
			try{
			getMeiJingSummary(ls);
			} catch(Exception e) {
				GrapErr ge = new  GrapErr();
				ge.setUrl(url);
				ge.setType("6");
				ge.setContent(JSON.toJSONString(ls));
				ge.setDescription("获取Summary 错误："+e.getMessage());
				insert2ErrDb(ge);
			}
		}

		if(lses.size() > 0) insert2LsDb(lses);
	}
	public void getMeiJingSummary(LifeScenery ls) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(ls.getSourceUrl());
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(ls.getSourceUrl());
			ge.setType("2");
			ge.setContent("美景");
			ge.setDescription("获取Deteal 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		Elements els = doc.select(".spots_info .type");
		if(null != els && !els.isEmpty()) {
			Element el = els.first();
			els = el.select("h1");
			if(null != els && !els.isEmpty()) {
				ls.setLevelName(els.first().getElementsByTag("em").text());
				els.first().getElementsByTag("em").remove();
				ls.setFullName(els.first().text().trim());
			}
			
			els = el.select("dl");
			if(null != els && !els.isEmpty()) {
				for(Element tel : els) {
					Elements tels = tel.getElementsByTag("dt");
					if(tels.text().contains("地址")) {
						ls.setAddress(tel.getElementsByTag("dd").text());
					} else if(tels.text().contains("开放时间")) {
						ls.setShopHours(tel.getElementsByTag("dd").text());
					} else if(tels.text().contains("景点类型")) {
						tels = tel.getElementsByTag("dd").first().getElementsByTag("a");
						String types = "";
						if(null != tels && !tels.isEmpty()) {
							for(Element ttel : tels) {
								if(types.length() > 0) types += ",";
								types += ttel.text();
								if(null == typeMap.get(ttel.text())) {
									typeMap.put(ttel.text(), getTypeId());
								}
							}
						}
						ls.setTypeName(types);
					} else if(tels.text().contains("门票信息")) {
						tels = tel.getElementsByTag("p");
						if(null != tels && !tels.isEmpty()) {
							ls.setTicketPrice(tels.attr("detail"));
						}
					}
				}
			}
			// 获取详情数据
			getMeiJingDetail(ls);
		}

	}
	public void getMeiJingDetail(LifeScenery ls) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(ls.getSourceUrl() + "profile");
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(ls.getSourceUrl() + "profile");
			ge.setType("3");
			ge.setContent("美景");
			ge.setDescription("获取Deteal 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		Elements els = doc.select(".details .type");
		if(null != els && !els.isEmpty()) {
			Element el = els.first();
			
			Elements tels = el.select(".top");
			if(null != tels && !tels.isEmpty()) {
//				int i = 0;// 1-门票；2：交通
//				StringBuffer sb = new StringBuffer();
//				for(Element tel : tels.first()..children()) {
//					if(tel.tagName().equals("b") && tel.text().contains("开放时间")) {
//						if(i == 2) {
//							ls.setTicketPrice(sb.toString());
//							sb = new StringBuffer();
//						}
//						i=1;
//						continue;
//					} else if(tel.tagName().equals("b") && tel.text().contains("交通概况")) {
//						if(i == 1) {
//							ls.setTraffic(sb.toString());
//							sb = new StringBuffer();
//						}
//						i = 2;
//						continue;
//					}
				tels.first().getElementsByTag("p").remove();
				String content = tels.first().html();
				boolean flag = false;
				StringBuffer traffic = new StringBuffer();
				for (int j = 0; j < content.length(); j++) {
		            char  item =  content.charAt(j);
		            if(flag == false && ">".equals(item+"") && content.length() > j+5) {
		            	String str = content.substring(j+1, j+5);
		            	if(str.equals("交通概况")) {
		            		flag = true;
		            		// <b><a href="jiaotong">交通概况</a>：</b>
		            		j += 13;
		            	}
		            	continue;
		            }
		            if(flag) {
		            	traffic.append(item);
		            }
		        }
				ls.setTraffic(traffic.toString());
			}
//				if(i == 1) {
//					ls.setTicketPrice(sb.toString());
//					sb = new StringBuffer();
//				} else if(i == 2) {
//					ls.setTraffic(sb.toString());
//					sb = new StringBuffer();
//				}
			tels.remove();
			el.getElementsByTag("b").remove();
			ls.setDescription(el.html());
		}
	}
	public void getMeiShiList(String url, String kind) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(kind);
			ge.setDescription("获取List 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		List<String> urls = new ArrayList<String>();
		Elements els = doc.select(".food_li .a a");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				urls.add("http://chongqing.cncn.com"+el.attr("href"));
			}
		}
		List<LifeFood> lis = new ArrayList<LifeFood>();
		for(String durl : urls) {
			LifeFood lf = getMeishiDetail(durl, kind);
			if(null != lf && null != lf.getId()) lis.add(lf);
		}
		
		if(lis.size() > 0) insert2LsfDb(lis);
	}

	public LifeFood getMeishiDetail(String url, String kind) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent("美食");
			ge.setDescription("获取Detail 错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		LifeFood lf = new  LifeFood();
		try {
			Elements els = doc.select("div.produce_info img");
			if(null != els && !els.isEmpty()) {
				lf.setCover(els.first().attr("src"));
			}
			
			els = doc.select("div.produce_info dl dd");
			if(null != els && !els.isEmpty()) {
				Elements tels = els.first().select(".title");
				if(null != tels && !tels.isEmpty()) {
					lf.setName(tels.select("h1").text());
				}
				tels.remove();
				lf.setDescription(els.text());
				lf.setKindName(kind);
				lf.setSourceUrl(url);
				lf.setSource("欣欣");
				lf.setId(getId());
			}
//			String url = li.getSourceUrl();
//			li.setOldId(url.substring(url.lastIndexOf("/")+1).replace(".html", ""));
			// 获取推荐商家信息链接
			els = doc.select("div.txt_tw ul li a");
			Elements imgels = doc.select("div.txt_tw ul li a img");
			
			if(null != imgels && !imgels.isEmpty()) {
				List<LifeFoodShop> lfss = new ArrayList<LifeFoodShop>();
				for(int i = 0; i< imgels.size(); i++) {
					LifeFoodShop lfs = new LifeFoodShop();
					lfs.setFoodId(lf.getId());
					lfs.setFoodName(lf.getName());
					lfs.setName(imgels.get(i).attr("alt"));
					lfs.setCover(imgels.get(i).attr("src"));
					// http://chongqing.cncn.com/store/1590/
					try {
						lfs.setOldId(Integer.valueOf(els.get(i).attr("href").substring(els.get(i).attr("href").lastIndexOf("store/")+6).replace("/", "")));
					} catch (Exception e) {
						e.printStackTrace();
					}
					lfss.add(lfs);
				}
				// 插入数据库
				insert2FoodShop(lfss);
			}
			return lf;
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(lf.getSourceUrl());
			ge.setType("4");
			ge.setContent(JSON.toJSONString(lf));
			ge.setDescription("获取Detail 内容错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
	}
	
	public void getMeijingList(String url, String kind) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(kind);
			ge.setDescription("获取List 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		List<LifeScenery> lis = new ArrayList<LifeScenery>();
		Elements els = doc.getElementsByAttributeValue("class", "list_mod2");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				LifeScenery ls = new LifeScenery();
				ls.setKindName(kind);
				// 获取封面
				Elements tempEls = el.select(".leftimg img");
				if(null != tempEls && !tempEls.isEmpty()) {
					ls.setCover(tempEls.first().attr("src"));
					ls.setCoverId(tempEls.first().attr("img-id"));
				}
				
//				tempEls = el.getElementsByAttributeValue("class", "rdetailbox");
				tempEls = el.select("div.rdetailbox dl dt a");
				if(null != tempEls && !tempEls.isEmpty()) {
					// 获取名称和链接
					ls.setName(tempEls.first().text());
					ls.setSourceUrl(baseUrl + tempEls.first().attr("href"));
					ls.setSource(source);
				}
				tempEls = el.select("div.rdetailbox dl dt s");
				if(null != tempEls && !tempEls.isEmpty()) {
					// 获取排名
					ls.setRank(tempEls.first().text());
				}
				tempEls = el.select("div.rdetailbox ul li .score strong");
				if(null != tempEls && !tempEls.isEmpty()) {
					// 获取得分
					ls.setScore(tempEls.first().text());
				}
				lis.add(ls);
			}
		}
		for(LifeScenery li : lis) {
			getDetail(li);
		}
		
		if(lis.size() > 0) insert2LsDb(lis);
	}

	public LifeScenery getDetail(LifeScenery li) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(li.getSourceUrl());
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(li.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(li));
			ge.setDescription("获取Detail 错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		try {
			Elements els = doc.getElementsByAttributeValue("class","s_sight_infor");
			if(null != els && !els.isEmpty()) {
				Elements tempEls = els.first().getElementsByAttributeValue("class", "s_sight_addr");
				if(null != tempEls && !tempEls.isEmpty()) {
					li.setAddress(tempEls.first().text().replace("地址：", ""));
				}
				els = els.first().getElementsByAttributeValue("class", "s_sight_in_list");
				if(null != els && !els.isEmpty()) {
					tempEls = els.first().getElementsByTag("li");
					if(null != tempEls && !tempEls.isEmpty()) {
						for(Element el  : tempEls) {
							String text = el.text();
							if(text.contains("类")) {
								Elements aEls = el.getElementsByTag("a");
								String type = "";
								if(null != aEls && !aEls.isEmpty()) {
									for(Element ael : aEls) {
										if(type.length() > 0) type += ";";
										type += ael.text();
									}
								}
								li.setCategoryName(type);
							} else if(text.contains("等")) {
								li.setLevelName(text.replace("等&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;级：", "").replace("等        级：", ""));
							} else if(text.contains("游玩")) {
								li.setPlaytime(text.replace("游玩时间：", ""));
							} else if(text.contains("电")) {
								li.setPhone(text.replace("电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话：", "").replace("电        话：", ""));
							} else if(text.contains("官方")) {
								li.setWebsite(text.replace("官方网站：", ""));
							}
						}
					}
				
					if(els.size() > 1) {
						tempEls = els.get(1).getElementsByTag("dd");
						if(null != tempEls && !tempEls.isEmpty()) {
							li.setShopHours(tempEls.first().text());
						}
					}
						
					if(els.size() > 2) {
						tempEls = els.get(2).getElementsByTag("dd");
						if(null != tempEls && !tempEls.isEmpty()) {
							li.setTicketPrice(tempEls.first().text());
						}
					}
				}
			}
			els = doc.select("div.toggle_l .text_style");
			if(null != els && !els.isEmpty()) {
				if(els.size() > 1) {
					li.setTips(getUtf8Str(els.get(1).html()));
				}
				li.setDescription(els.get(0).html());
			}
			String url = li.getSourceUrl();
			li.setOldId(url.substring(url.lastIndexOf("/")+1).replace(".html", ""));
			// 获取交通信息链接
			for(Element el : doc.getElementById("jieshao").getElementsByTag("a")) {
				if(el.text().contains("交通")) {
					li.setTraffic(getTraffic(baseUrl + el.attr("href"), li.getOldId()));
					break;
				}
			}
			
			els = doc.select("dl.comment_show dd");
			if(null != els && !els.isEmpty()) {
				for(Element el : els) {
					String text = el.text();
					if(text.contains("景")) {
						li.setScenery(text.replace("景&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;色", "").replace("景 色 ", ""));
					} else if(text.contains("趣")) {
						li.setInterest(text.replace("趣&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;味", "").replace("趣 味", ""));
					} else if(text.contains("性价比")) {
						li.setCost(text.replace("性价比", ""));
					}
				}
			}
			return li;
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(li.getSourceUrl());
			ge.setType("4");
			ge.setContent(JSON.toJSONString(li));
			ge.setDescription("获取Detail 内容错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
	}
	
	public String getTraffic(String url, String oid) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent("获取链接错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return "";
		}

		if (null == doc) return "";
		try {
			Elements els = doc.getElementsByAttributeValue("class","text_style");
			return (els == null || els.size() == 0) ? "" : els.first().html();
			
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("7");
			ge.setContent(oid);
			ge.setDescription("获取交通信息错误：" + e.getMessage());
			insert2ErrDb(ge);
			return "";
		}
	}
	
	
	public void insert2LsDb(List<LifeScenery> lses) {
		String sql = "insert into life_scenery(playtime,source_url,category_name,type_name,kind_name,type_ids,score,ticket_price,rank,tips,address,images,category_ids,shop_hours,source,cover,traffic,website,province_name,name,phone,description,cost,interest,scenery,city_name,level_name,full_name) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(LifeScenery cc : lses) {
				
				ps.setString(1, cc.getPlaytime());
				ps.setString(2, cc.getSourceUrl());
				ps.setString(3, cc.getCategoryName());
				ps.setString(4, cc.getTypeName());
				ps.setString(5, cc.getKindName());
				ps.setString(6, null);
				ps.setString(7, cc.getScore());
				ps.setString(8, cc.getTicketPrice());
				ps.setString(9, cc.getRank());
				ps.setString(10, cc.getTips());
				ps.setString(11, cc.getAddress());
				ps.setString(12, cc.getImages());
				ps.setString(13, null);
				ps.setString(14, cc.getShopHours());
				ps.setString(15, cc.getSource());
				ps.setString(16, cc.getCover());
				ps.setString(17, cc.getTraffic());
				ps.setString(18, cc.getWebsite());
				ps.setString(19, cc.getProvinceName());
				ps.setString(20, cc.getName());
				ps.setString(21, cc.getPhone());
				ps.setString(22, cc.getDescription());
				ps.setString(23, cc.getCost());
				ps.setString(24, cc.getInterest());
				ps.setString(25, cc.getScenery());
				ps.setString(26, cc.getCityName());
				ps.setString(27, cc.getLevelName());
				ps.setString(28, cc.getFullName());

//				ps.setString(1, ls.getOldId());
//				ps.setString(2, ls.getPlaytime());
//				ps.setInt(3, 0);
//				ps.setString(4, ls.getSourceUrl());
//				ps.setString(5, ls.getCategoryName());
//				ps.setInt(6, null == ls.getCityId() ? 0 : ls.getCityId());
//				ps.setString(7, ls.getTypeName());
//				ps.setString(8, ls.getKindName());
//				ps.setString(9, ls.getScore());
//				ps.setString(10, ls.getTicketPrice());
//				ps.setString(11, ls.getRank());
//				ps.setString(12, ls.getTips());
//				ps.setString(13, ls.getAddress());
//				ps.setString(14, null);
//				ps.setString(15, ls.getShopHours());
//				ps.setInt(16, null == ls.getProvinceId() ? 0 : ls.getProvinceId());
//				ps.setString(17, ls.getSource());
//				ps.setString(18, ls.getCoverId());
//				ps.setString(19, ls.getCover());
//				ps.setString(20, ls.getTraffic());
//				ps.setString(21, ls.getWebsite());
//				ps.setString(22, ls.getProvinceName());
//				ps.setString(23, ls.getName());
//				ps.setString(24, null);
//				ps.setString(25, null);
//				ps.setString(26, null);
//				ps.setString(27, ls.getPhone());
//				ps.setString(28, ls.getDescription());
//				ps.setInt(29, null == ls.getKindId() ? 0 : ls.getKindId());
//				ps.setString(30, ls.getCityName());
//				ps.setString(31, ls.getLevelName());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lses));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public void insert2LsfDb(List<LifeFood> lfs) {
		String sql = "insert into life_food(source_url,kind_name,cover,province_name,images,name,description,city_name,source,id) values(?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(LifeFood cc : lfs) {
				ps.setString(1, cc.getSourceUrl());
				ps.setString(2, cc.getKindName());
				ps.setString(3, cc.getCover());
				ps.setString(4, cc.getProvinceName());
				ps.setString(5, cc.getImages());
				ps.setString(6, cc.getName());
				ps.setString(7, cc.getDescription());
				ps.setString(8, cc.getCityName());
				ps.setString(9, cc.getSource());
				ps.setInt(10, cc.getId());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lfs));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	
	private String getUtf8Str(String str) {
		try {
			str = null == str ? null : new String(str.getBytes(), "utf-8").replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "").replaceAll("[\\x{10000}-\\x{10FFFF}]", "");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public void insert2FoodShop(List<LifeFoodShop> lfss) {
		PreparedStatement grapPs = null;
		try {
			
			grapPs = DbUtil.getComConn().prepareStatement("insert into life_food_shop(name,food_id,food_name,cover,old_id) values(?,?,?,?,?)");
			for(LifeFoodShop lfs : lfss) {
				grapPs.setString(1, lfs.getName());
				grapPs.setInt(2, lfs.getFoodId());
				grapPs.setString(3, lfs.getFoodName());
				grapPs.setString(4, lfs.getCover());
				grapPs.setInt(5,null ==  lfs.getOldId() ? 0 : lfs.getOldId());
				grapPs.addBatch();
			}
			grapPs.executeBatch();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public void insert2ShopDb(List<ComShop> shops) {
		String sql = "insert into com_shop(old_id,category_id,source_url,category_name,city_id,type_name,kind_name,address,flavor,second_name,qualification,province_id,tags,shop_hours,full_name,name,source,star,menu,cover,brand_story,province_name,district_name,place_name,service,spend_perhead,type_id,environment,phone,description,kind_id,list,city_name,traffic) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(ComShop ls : shops) {
				ps.setString(1, ls.getOldId());
				ps.setInt(2, null == ls.getCategoryId() ? 0 : ls.getCategoryId() );
				ps.setString(3, ls.getSourceUrl());
				ps.setString(4, ls.getCategoryName());
				ps.setInt(5, null == ls.getCityId() ? 0 : ls.getCityId() );
				ps.setString(6, ls.getTypeName());
				ps.setString(7, ls.getKindName());
				ps.setString(8, ls.getAddress());
				ps.setString(9, ls.getFlavor());
				ps.setString(10, ls.getSecondName());
				ps.setString(11, ls.getQualification());
				ps.setInt(12, null == ls.getProvinceId() ? 0 : ls.getProvinceId() );
				ps.setString(13, ls.getTags());
				ps.setString(14, ls.getShopHours());
				ps.setString(15, ls.getFullName());
				ps.setString(16, ls.getName());
				ps.setString(17, ls.getSource());
				ps.setString(18, ls.getStar());
				ps.setString(19, ls.getMenu());
				ps.setString(20, ls.getCover());
				ps.setString(21, ls.getBrandStory());
				ps.setString(22, ls.getProvinceName());
				ps.setString(23, ls.getDistrictName());
				ps.setString(24, ls.getPlaceName());
				ps.setString(25, ls.getService());
				ps.setString(26, ls.getSpendPerhead());
				ps.setInt(27, null == ls.getTypeId() ? 0 : ls.getTypeId() );
				ps.setString(28, ls.getEnvironment());
				ps.setString(29, ls.getPhone());
				ps.setString(30, ls.getDescription());
				ps.setInt(31, null == ls.getKindId() ? 0 : ls.getKindId() );
				ps.setString(32, ls.getList());
				ps.setString(33, ls.getCityName());
				ps.setString(34, ls.getTraffic());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("14");
			ge.setContent(JSON.toJSONString(shops));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static List<GrapErr> getErrs() {
		List<GrapErr> univs = new ArrayList<GrapErr>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select * from grap_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				GrapErr univ = new GrapErr();
				univ.setId(rs.getInt("id"));
				univ.setUrl(rs.getString("url"));
				univ.setType(rs.getString("type"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	
	public static void delErr(int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("delete from grap_err where id=?");
			grapPs.setInt(1, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static void updateLs(LifeScenery ls) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("update life_scenery set cost =?,interest =?,scenery =?  where id=?");
			grapPs.setString(1, ls.getCost());
			grapPs.setString(2, ls.getInterest());
			grapPs.setString(3, ls.getScenery());
			grapPs.setInt(4, ls.getId());
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static Map<Integer, String> getSensList() {
		Map<Integer, String> map =new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select  id,source_url from life_scenery";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				map.put(rs.getInt("id") ,rs.getString("source_url"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	private static synchronized int getId() {
		id.addAndGet(1);
		return id.get();
	}
	
	public static int getMaxId() {
		int r = 1;
		try {
			Statement statement = DbUtil.getComConn().createStatement();
			String sql = "select max(id) as id from life_food";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				r = rs.getInt("id");
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
}
